package tliasmanagement.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tliasmanagement.anno.Log;
import tliasmanagement.pojo.Dept;
import tliasmanagement.pojo.Result;
import tliasmanagement.service.DeptService;

import java.util.List;

@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    /*
     * 查询所有部门
     * */
    @GetMapping
    public Result list() {
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    /*
     * 删除部门
     * */
    @Log
    @DeleteMapping
    public Result delete(Integer id) {
        deptService.deleteById(id);
        return Result.success();
    };

    /*
    * 添加部门
    * */
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        deptService.add(dept);
        return Result.success();
    }

    /*
    * 查询部门
    * */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Dept dept = deptService.findById(id);
        return Result.success(dept);
    }

    /*
    * 修改部门
    * */
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        deptService.update(dept);
        return Result.success();
    }
}
